package cn.app.web.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.mail.Multipart;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.app.entity.AppCategory;
import cn.app.entity.AppInfo;
import cn.app.entity.DataDictionary;
import cn.app.entity.DevUser;
import cn.app.service.AppCategoryService;
import cn.app.service.AppInfoService;
import cn.app.service.DataDictionaryService;
import cn.app.utils.PageBean;



	

@Controller
@RequestMapping("/devApp")
public class DevAppController {
		@Autowired
		private DataDictionaryService dataDictionaryService;
		
		@Autowired
		private AppCategoryService appCategoryService;
		
		@Autowired
		private AppInfoService appInfoService;
	
	@RequestMapping("/disMain")
	public String disMain(){
		
		return "dev/main";
	}
	
	@RequestMapping("/appInfoList")
	public String appInfoList(Model model,
							@RequestParam(value="softwareName",required=false)String softwareName,
							@RequestParam(value="appStatus",required=false)String status,
							@RequestParam(value="appFlatform",required=false)String flatform,
							@RequestParam(value="appLevelOne",required=false)String levelOne,
							@RequestParam(value="appLevelTwo",required=false)String levelTwo,
							@RequestParam(value="appLevelThree",required=false)String levelThree,
							@RequestParam(value="currentPage",required=false)Integer currentPage,
							@RequestParam(value="pageSize",required=false)Integer pageSize)	{
																												
		//4.数据回显
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("softwareName",softwareName=softwareName==null?"":softwareName.trim());
		map.put("status", status);
		map.put("flatform", flatform);
		map.put("levelOne", levelOne);
		map.put("levelTwo", levelTwo);
		map.put("levelThree", levelThree);
		map.put("currentPage", currentPage);
		map.put("pageSize", pageSize);
		//4.放到model中
		model.addAttribute("map", map);
		
		//5.返回指定的二级分类和三级分类，返回给jsp页面(回显数据)
		if(!StringUtils.isEmpty(levelOne)){
			List<AppCategory> levelTwoList=appCategoryService.findAllByPraent(levelOne);
			//5.放到model中
			model.addAttribute("levelTwoList", levelTwoList);
		}
		if(!StringUtils.isEmpty(levelTwo)){
			List<AppCategory> levelThreeList=appCategoryService.findAllByPraent(levelTwo);
			//5.放到model中
			model.addAttribute("levelThreeList", levelThreeList);
		}
		//放到model中
		model.addAttribute("map", map);
		
		//1.查询App状态
		List<DataDictionary> statusList=dataDictionaryService.findAll("APP_STATUS");
		
		//1.放到model中
		model.addAttribute("statusList", statusList);
		
		//2.查询所属平台
		List<DataDictionary> flatformList=dataDictionaryService.findAll("APP_FLATFORM");
		
		//2.放到model里
		model.addAttribute("flatformList", flatformList);
		
		//3.查询一级分类
		List<AppCategory> levelOneList=appCategoryService.findAllByPraent(null);
		
		//3.放到model中
		model.addAttribute("levelOneList", levelOneList);
		
		//6.查询App全部信息
		PageBean<AppInfo> pageBean=appInfoService.findAllByCondition(map);
		
		//6.放到model里
		model.addAttribute("pageBean",pageBean);
		
		return "dev/appInfoList";
	}
	
	
	@RequestMapping("/findLevelJson")
	public @ResponseBody List<AppCategory> findLevelJson(@RequestBody Map<String, String> map){
		
		List<AppCategory> levelOneList=appCategoryService.findAllByPraent(map.get("levelId"));
		System.out.println(levelOneList);
		return levelOneList;
	}
	
	//跳转到新增app基础信息页面
	@RequestMapping("/disAppInfoAdd")
	public String disAppInfoAdd(Model model){
		//2.查询所属平台
		List<DataDictionary> flatformList=dataDictionaryService.findAll("APP_FLATFORM");
				
		//2.放到model里
		model.addAttribute("flatformList", flatformList);
				
		//3.查询一级分类
		List<AppCategory> levelOneList=appCategoryService.findAllByPraent(null);
				
		//3.放到model中
		model.addAttribute("levelOneList", levelOneList);
		
		
		
		return "dev/appInfoAdd";
	}
	//异步校验APK名称
	@RequestMapping("/checkAPKName")
	public @ResponseBody Map<String, String> checkAPKName(@RequestBody Map<String, String> map){
		//获得APK名称
		String apkName=map.get("aplName");
		//调用service去数据库查询
		Integer count=appInfoService.findAppInfoByAPKName(apkName);
		Map<String, String> resultMap=new HashMap<>();
	
			if(count>0){
				resultMap.put("apkNameInfo", "已经存在....");
			
				
			}
			
			
			return resultMap;
		}
		
		
	
	
	//新增APP基础信息
	@RequestMapping(value="/appInfoAdd",method=RequestMethod.POST)
	public String appInfoAdd(AppInfo appInfo,MultipartFile picFile,Model model,HttpSession session) throws Exception{
		
	
		
		/*if(!(picFile.getSize() >(50*1024))){
			//图片太大，应该小于50KB     知道有这事就行
			
		}
		*/
		if(picFile.getSize() >0){
			//获得原名字
			String originalName = picFile.getOriginalFilename();
			//校验图片必须是.jpg/.png/.jpeg
			String typeName = originalName.substring(originalName.indexOf("."));
			//判断
			if(!(".jpg".equals(typeName)||".png".equals(typeName)||".jpeg".equals(typeName))){
				//图片格式不正确
				model.addAttribute("picInfo", "图片格式不正确");
				return "forward:/devApp/disappInfoAdd";
				
			}
			//保存图片
			//设置名字
			String newName=UUID.randomUUID().toString()+typeName;
			//保存/转移
			picFile.transferTo(new File("D:\\pic\\"+newName));
			
			appInfo.setLogopicpath("http://localhost:8080/pic/"+newName);
			appInfo.setLogolocpath("D:\\pic\\"+newName);
			
			
		}
		/*DevUser devUser=(DevUser) session.getAttribute("devUser");
		appInfo.setDevid(devUser.getId());
		appInfo.setCreatedby(devUser.getId());*/
		Integer count=appInfoService.addAppInfo(appInfo);
		if(count >0){
			//添加成功
			return "redirect:/devApp/appInfoList";
		}else{
			//添加失败
			model.addAttribute("addInfo", "添加失败,系统正在更新，请联系管理员");
			return "forward:/devApp/disappInfoAdd";
		}
		
		
	}

	
	
	
	
}
